/*
 * Created on 2006-8-7
 *
 * TODO 
 */
package com.powerunion.datacollection.report.excelreport;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.powerunion.datacollection.report.excelreport.config.ReportConfig;


/**
 * <b>&nbsp;&nbsp;&nbsp;&nbsp;TODO:</b> 
 * ExcelReportServlet 是报表生成器在Web服务器上的一个应用封装，通过这个封装好的Servlet
 * 用户就不需要再去封装自己的Servlet。只需将该Servlet部署到相应的应用中去，即可以在页面直接请求报表
 * 打印。<br>
 * &nbsp;&nbsp;&nbsp;&nbsp;<b>如何配置该Servlet：</b>(该servlet支持以下几个输入参数)<br>
 * &nbsp;&nbsp;&nbsp;&nbsp;
 * 1. TempletFilePath：设定模板文件和配置文件(ReportConfig.xml)目录(相对于webroot目录的逻辑目录名).<br>
 * &nbsp;&nbsp;&nbsp;&nbsp;
 * 2. DSImplDataBase： 配置数据库类型数据源实现类，(用户自定义的实现,该类必须继承至DBDataSource类)<br>
 * &nbsp;&nbsp;&nbsp;&nbsp;
 * 3. DSImplStatic：配置静态类型数据源实现类.(用户自定义的实现,该类必须继承至BaseDataSource类)<br>
 * &nbsp;&nbsp;&nbsp;&nbsp;
 * 4. JNDI：如果用户没有配置‘DSImplDataBase’可以配置JNDI来使用默认的数据库类型数据源来实现数据检索。<br>
 * &nbsp;&nbsp;&nbsp;&nbsp;
 * 5. Logger: 配置Excel报表生成器的日志输出实现类，该类必须实现Logger接口
 * @author juny
 * @version 1.0
 * @since 1.0
 * @see net.excel.report.Logger
 * @see net.excel.report.datasource.DBDataSource
 * @see net.excel.report.datasource.BaseDataSource
 */
public class ExcelReportServlet extends BaseExcelReportServlet {
    private static Logger log = Logger.getLogger(ExcelReportServlet.class);
    
    /**
     * 处理报表打印请求。这里控制了同时请求报表处理请求的个数。
     * @param request
     * @param response
     * @throws IOException
     */
    protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException{
    	String realPath = request.getSession().getServletContext().getRealPath("");
    	String xlsPath = realPath+"\\report\\xls\\";
    	
        String reportName = (String)request.getParameter(ReportConfig.REPORT);
        String reloadConfig = (String)request.getParameter("reloadConfig");
        boolean needReloadConfig = false;
        if(null != reloadConfig && reloadConfig.equalsIgnoreCase("true")){
            needReloadConfig = true;
            if(log.isDebugEnable()){
                log.debug("reload config ");
            }
        }
        File file = new File(xlsPath+reportName+".xls");
        file.createNewFile();
        file.mkdir();
        if(file.isFile()){
        	System.out.println("是File......");
        }else{
        	System.out.println("不是File......");
        }
        FileOutputStream os = new FileOutputStream(file,true);
        try{

            long curTime = System.currentTimeMillis();
            ReportEngine r = getReportEngine(needReloadConfig);
            //取得报表参数
            Map params = getParamMap(request);
            //生成报表
            r.getReportAsExcel(os, params);
            os.flush();
            log.debug("Total time:" + String.valueOf(System.currentTimeMillis() - curTime) + " milliseconds");
            
            //response.flushBuffer();
        }catch(Exception e){
            log.error(e.getMessage(), e);
            response.setContentType("text/html");
            response.setHeader("Content-Encoding","gb2312");
            
            PrintWriter buf = new PrintWriter(os);
            buf.println("<HTML>");
            buf.println("<HEAD><TITLE>Excel Report debug</TITLE></HEAD>");
            buf.println("<BODY>");
            buf.println("<b>异常信息：</b><br>");
            buf.println(e.getMessage());
            buf.println("</BODY></HTML>");
            os.flush();
            
        }finally{
        	if(os != null){
        		os.close();
        		os = null;
        	}
        }
    }
}
